<?php
require_once(dirname(__FILE__) . '/PortalUtil.class.php');

class Black extends PortalUtil {
	public $tbl = Constant::tbl_wifi_blacklist;

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function layout($controller, $request) {
		$this->pretreat($controller);
		
		$this->nav['home'] = true;
		
		$controller->display($request, 'index');
		return true;
	}
	
	function is_black($request, $user) {
		
		$uid = $user['id'];
		$mac = $request['mac'];
		
		return $this->_is_black($uid, $mac);
	}
	
	function _is_black($uid, $mac) {
		
		if (empty($username) || empty($mac)) {
			return false;
		}
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE uid = '%s' AND mac = '%s'";
		$sql = sprintf($fmt, $this->tbl, $uid, $mac);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['status'] == '2') {
				return true;
			}
		}
		
		return false;
	}
	
	function _update($user, $mac, $status) {
		
		$office = $user['office'];
		$dept = $user['dept'];
		$emp = $user['emp'];
		
		$uid = $user['id'];
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE uid = '%s' AND mac = '%s'";
		$sql = sprintf($fmt, $this->tbl, $uid, $mac);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$fmt = "UPDATE %s SET status = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $status, $row['id']);
		} else {
			$fmt = "INSERT INTO %s(office, dept, emp, uid, mac, status, createtime, modifytime) 
					VALUES('%s', '%s', '%s', '%s', '%s', '%s', NOW(), NOW())";
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $uid, $mac, $status);
		}
		debug($sql);
		$db->query($sql);
	}
}
?>
